﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Globalization;
using System.Web.Security;

public partial class AdminPage : System.Web.UI.Page
{
    ServerObject mServerObject = null;

    protected override void InitializeCulture()
    {
        System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(HttpContext.Current.Request.UserLanguages[0]);
        System.Threading.Thread.CurrentThread.CurrentUICulture =  new  CultureInfo(HttpContext.Current.Request.UserLanguages[0]);
       
        base.InitializeCulture();
    }


    private bool CheckServerObject()
    {
        bool result = false;        

        if (Session["ServerModule"] != null)
        {
            mServerObject = (ServerObject)Session["ServerModule"];
        }

        if (mServerObject != null)
        {
            result = true;
        }

        return result;
    }

    private void ShowPanel(string panelName)
    {      
        #region Hide panels

        templateZIPanel.Visible = false;
        addNewTemplateZiPanel.Visible = false;
        editTemplateZiPanel.Visible = false;


        calendarPanel.Visible = false;
        editCalendarPanel.Visible = false;
        fillFromTemplatePanel.Visible = false;

        HolyDaysPanel.Visible = false;
        editHolidaysPanel.Visible = false;
        addNewHolidayPanel.Visible = false;

        VisualizareProgramariPanel.Visible = false;
        #endregion Hide panels

        try
        {
            if (CheckServerObject())
            {
                #region Get Panel Name

                switch (panelName)
                { 
                    #region Teplate ZI
                    case "templateZIPanel":
                        templateZIPanel.Visible = true;
                        mServerObject.InfoText(this, "Template ZI");

                        if (!IsPostBack)
                        {
                            DataTable categoryList = mServerObject.GetConsulatList();
                            mServerObject.fillSelector(templateZiConsulatSelector, categoryList, "Denumire", "ID Consulat");

                            DataTable serviceList = mServerObject.GetServicesCategoryList();
                            mServerObject.fillSelector(templateZiServiceDDL, serviceList, "Denumirea", "ID Service");

                            DataTable userData = (DataTable)Session["UserData"];
                            if (userData != null && userData.Rows.Count == 1 && userData.Columns.Contains("IdConsulat"))
                            {
                                int consulatID = 0;
                                int.TryParse(userData.Rows[0]["IdConsulat"].ToString(), out consulatID);

                                if (consulatID != 0)
                                {
                                    templateZiConsulatSelector.SelectedValue = consulatID.ToString();

                                    if (!userData.Rows[0]["RoleType"].ToString().Equals("1"))
                                        templateZiConsulatSelector.Enabled = false;
                                    else
                                    {
                                        templateZiConsulatSelector.Enabled = true;
                                    }
                                }                               
                            }
                        }

                        FillTemplateZiGridView(templateZiConsulatSelector.SelectedValue, templateZiServiceDDL.SelectedValue, templateZiZiSaptaminaDropDownList.SelectedValue);

                        break;

                    case "addNewTemplateZiPanel":
                        addNewTemplateZiPanel.Visible = true;
                        {
                            DataTable consList = mServerObject.GetConsulatList();
                            mServerObject.fillSelector(addNewTemplateZiConsulatDDL, consList, "Denumire", "ID Consulat");

                            DataTable defaultTempates = mServerObject.GetDefaultTemplatesList();
                            mServerObject.fillSelector(addNewTemplateFromDefaultTemplateDDL, defaultTempates, "idPriod", "idPriod");

                            DataTable userData = (DataTable)Session["UserData"];
                            if (userData != null && userData.Rows.Count == 1 && userData.Columns.Contains("IdConsulat"))
                            {
                                int consulatID = 0;
                                int.TryParse(userData.Rows[0]["IdConsulat"].ToString(), out consulatID);

                                if (consulatID != 0)
                                {
                                    addNewTemplateZiConsulatDDL.SelectedValue = consulatID.ToString();

                                    if (!userData.Rows[0]["RoleType"].ToString().Equals("1"))
                                        addNewTemplateZiConsulatDDL.Enabled = false;
                                    else
                                    {
                                        addNewTemplateZiConsulatDDL.Enabled = true;
                                    }
                                }                               
                            }

                            DataTable categoryList = mServerObject.GetServicesCategoryList();
                            mServerObject.fillSelector(addNewTemplateZiServiceCategoryDDL, categoryList, "Denumirea", "ID Service");
                        }
                        break;

                    case "editTemplateZiPanel":
                        editTemplateZiPanel.Visible = true;
                        {
                            DataTable consList = mServerObject.GetConsulatList();
                            mServerObject.fillSelector(editTemplateZiConsulatDDL, consList, "Denumire", "ID Consulat");

                            DataTable userData = (DataTable)Session["UserData"];
                            if (userData != null && userData.Rows.Count == 1 && userData.Columns.Contains("IdConsulat"))
                            {
                                int consulatID = 0;
                                int.TryParse(userData.Rows[0]["IdConsulat"].ToString(), out consulatID);

                                if (consulatID != 0)
                                {
                                    editTemplateZiConsulatDDL.SelectedValue = consulatID.ToString();

                                    if (!userData.Rows[0]["RoleType"].ToString().Equals("1"))
                                        editTemplateZiConsulatDDL.Enabled = false;
                                    else
                                    {
                                        editTemplateZiConsulatDDL.Enabled = true;
                                    }
                                }                               
                            }

                            DataTable categoryList = mServerObject.GetServicesCategoryList();
                            mServerObject.fillSelector(editTemplateZiServiceCategoryDDL, categoryList, "Denumirea", "ID Service");
                        }
                        break;

                    #endregion Template ZI

                    #region Calendar

                    case "calendarPanel":
                        calendarPanel.Visible = true;
                        {
                            DataTable consList = mServerObject.GetConsulatList();
                            mServerObject.fillSelector(calendarConsulatDDL, consList, "Denumire", "ID Consulat");

                            DataTable userData = (DataTable)Session["UserData"];
                            if (userData != null && userData.Rows.Count == 1 && userData.Columns.Contains("IdConsulat"))
                            {
                                int consulatID = 0;
                                int.TryParse(userData.Rows[0]["IdConsulat"].ToString(), out consulatID);

                                if (consulatID != 0)
                                {
                                    calendarConsulatDDL.SelectedValue = consulatID.ToString();

                                    if (!userData.Rows[0]["RoleType"].ToString().Equals("1"))
                                        calendarConsulatDDL.Enabled = false;
                                    else
                                    {
                                        calendarConsulatDDL.Enabled = true;
                                    }
                                }                                
                            }
                        }
                        break;

                    case "editCalendarPanel":
                        editCalendarPanel.Visible = true;                        
                        break;

                    case "fillFromTemplatePanel":
                        fillFromTemplatePanel.Visible = true;
                        {
                            DataTable consList = mServerObject.GetConsulatList();
                            mServerObject.fillSelector(fillFromTemplateConsulatDDL, consList, "Denumire", "ID Consulat");

                            DataTable serviceList = mServerObject.GetServicesCategoryList();
                            mServerObject.fillSelector(fillFromTemplateServiceDDL, serviceList, "Denumirea", "ID Service");
                            
                            DataTable userData = (DataTable)Session["UserData"];
                            if (userData != null && userData.Rows.Count == 1 && userData.Columns.Contains("IdConsulat"))
                            {
                                int consulatID = 0;
                                int.TryParse(userData.Rows[0]["IdConsulat"].ToString(), out consulatID);

                                if (consulatID != 0)
                                {
                                    fillFromTemplateConsulatDDL.SelectedValue = consulatID.ToString();

                                    if (!userData.Rows[0]["RoleType"].ToString().Equals("1"))
                                        fillFromTemplateConsulatDDL.Enabled = false;
                                    else
                                    {
                                        fillFromTemplateConsulatDDL.Enabled = true;
                                    }
                                }                               
                            }

                            FillDateFromTo();
                        }
                        break;
                    
                    #endregion Calendar

                    #region Sarbatori
                    case "HolyDaysPanel":
                        HolyDaysPanel.Visible = true;
                        {
                            DataTable consList = mServerObject.GetConsulatList();
                            mServerObject.fillSelector(holidayConsulatDDL, consList, "Denumire", "ID Consulat");

                            DataTable userData = (DataTable)Session["UserData"];
                            if (userData != null && userData.Rows.Count == 1 && userData.Columns.Contains("IdConsulat"))
                            {
                                int consulatID = 0;
                                int.TryParse(userData.Rows[0]["IdConsulat"].ToString(), out consulatID);

                                if (consulatID != 0)
                                {
                                    holidayConsulatDDL.SelectedValue = consulatID.ToString();
                                    
                                    if (!userData.Rows[0]["RoleType"].ToString().Equals("1"))
                                        holidayConsulatDDL.Enabled = false;
                                    else
                                    {
                                        holidayConsulatDDL.Enabled = true;
                                    }
                                }                                
                            }

                            FillHolydaysGridView(holidayConsulatDDL.SelectedValue);
                        }
                        break;

                    case "editHolidaysPanel":
                        editHolidaysPanel.Visible = true;
                        {
                            DataTable consList = mServerObject.GetConsulatList();
                            mServerObject.fillSelector(editHolidaysConsulatDDL, consList, "Denumire", "ID Consulat");

                            DataTable userData = (DataTable)Session["UserData"];
                            if (userData != null && userData.Rows.Count == 1 && userData.Columns.Contains("IdConsulat"))
                            {
                                int consulatID = 0;
                                int.TryParse(userData.Rows[0]["IdConsulat"].ToString(), out consulatID);

                                if (consulatID != 0)
                                {
                                    editHolidaysConsulatDDL.SelectedValue = consulatID.ToString();

                                    if (!userData.Rows[0]["RoleType"].ToString().Equals("1"))
                                        editHolidaysConsulatDDL.Enabled = false;
                                    else
                                    {
                                        editHolidaysConsulatDDL.Enabled = true;
                                    }
                                }
                               
                            }
                        }
                        break;

                    case "addNewHolidayPanel":
                        addNewHolidayPanel.Visible = true;
                        {
                            DataTable consList = mServerObject.GetConsulatList();
                            mServerObject.fillSelector(addNewHolidayConsulatDDL, consList, "Denumire", "ID Consulat");

                            DataTable userData = (DataTable)Session["UserData"];
                            if (userData != null && userData.Rows.Count == 1 && userData.Columns.Contains("IdConsulat"))
                            {
                                int consulatID = 0;
                                int.TryParse(userData.Rows[0]["IdConsulat"].ToString(), out consulatID);

                                if (consulatID != 0)
                                {
                                    addNewHolidayConsulatDDL.SelectedValue = consulatID.ToString();
                                    
                                    if (!userData.Rows[0]["RoleType"].ToString().Equals("1"))
                                        addNewHolidayConsulatDDL.Enabled = false;
                                    else
                                    {
                                        addNewHolidayConsulatDDL.Enabled = true;
                                    }
                                }
                               
                            }
                        }
                        break;

                    #endregion Sarbatori

                    #region Sumarul Programarilor

                    case "VisualizareProgramariPanel":
                        VisualizareProgramariPanel.Visible = true;
                        {
                            DataTable consList = mServerObject.GetConsulatList();
                            mServerObject.fillSelector(VisualizareProgramariConsulatDDL, consList, "Denumire", "ID Consulat");

                            DataTable serviceList = mServerObject.GetServicesCategoryList();
                            mServerObject.fillSelector(VisualizareProgramariServiceDDL, serviceList, "Denumirea", "ID Service");

                            DataTable userData = (DataTable)Session["UserData"];
                            if (userData != null && userData.Rows.Count == 1 && userData.Columns.Contains("IdConsulat"))
                            {
                                int consulatID = 0;
                                int.TryParse(userData.Rows[0]["IdConsulat"].ToString(), out consulatID);

                                if (consulatID != 0)
                                {
                                    VisualizareProgramariConsulatDDL.SelectedValue = consulatID.ToString();

                                    if (!userData.Rows[0]["RoleType"].ToString().Equals("1"))
                                        VisualizareProgramariConsulatDDL.Enabled = false;
                                    else
                                    {
                                        VisualizareProgramariConsulatDDL.Enabled = true;
                                    }
                                }
                            }

                            FillVisualizareProgramarilorGridView();
                        }
                        break;


                    #endregion Sumarul Programarilor

                    default:
                        break;
                }   
                
                #endregion Get Panel Name
            }
        }
        catch (Exception exception)
        {
            mServerObject.LogFatal(exception.Message);
        }
    }

    protected void FillDateFromTo()
    {
        int idConsulat = 0;
        int.TryParse(fillFromTemplateConsulatDDL.SelectedValue, out idConsulat);

        int idService = 0;
        int.TryParse(fillFromTemplateServiceDDL.SelectedValue, out idService);

        DateTime lastCompletedDay = mServerObject.GetMaxCalendarDataRegistred(idConsulat, idService);
        fillCalendarFromTemplateBeginFromTextBox.Text = lastCompletedDay.ToShortDateString();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        #region LogOut Action
        if (Request["action"] != null)
        {
            string action = Request["action"].ToString();

            switch (action)
            {
                case "logout":
                    Session["UserData"] = null;
                    Session["ServerModule"] = null;
                    Response.Redirect("Default.aspx", true);
                    break;
                default:
                    break;
            }
        }
        #endregion LogOut ACtion

        string areaGUID = string.Empty;
        if (Request["area"] != null)
        {
            areaGUID = Request["area"].ToString();
        }

        DataTable userData = (DataTable)Session["UserData"];

        #region Display Panels depending of area
        if (userData != null && userData.Rows.Count > 0)
        {
            if (CheckServerObject())
            {
                if (!IsPostBack)
                {
                    string areaName = areaGUID; //mServerObject.GetAreaName(areaGUID);
                    // area for each of administration entry points
                    switch (areaName)
                    {
                        case "templatezi":
                            ShowPanel(templateZIPanel.ID);
                            break;

                        case "calendar":
                            ShowPanel(calendarPanel.ID);
                            break;

                        case "sarbatori":
                            ShowPanel(HolyDaysPanel.ID);
                            break;

                        case "programari":
                            ShowPanel(VisualizareProgramariPanel.ID);
                            break;

                        default:
                            ShowPanel(string.Empty);
                            mServerObject.InfoText(this, "Please select area from left panel menu");
                            break;
                    }
                }
            }
        }

        #endregion Display Panels depending of area
    }
    
    #region Template ZI

    protected void FillTemplateZiGridView(string idConsulat, string idService, string ziSaptamina)
    {
        if (CheckServerObject())
        {
            int intIDConsulat = 0;
            int intSaptamina = 0;
            int intIDService = 0;

            bool ziSaptaminaStateCheked = templateZIUseWeekFilterCheckBox.Checked;
            bool idServiceStateCheked = templateZiUseServiceFilterCheckBox.Checked;

            int.TryParse(idConsulat, out intIDConsulat);
            if (ziSaptaminaStateCheked) int.TryParse(ziSaptamina, out intSaptamina);
            if (idServiceStateCheked) int.TryParse(idService, out intIDService);

            DataTable templZiDT = mServerObject.GetTemplatesList(intIDConsulat, intIDService, intSaptamina);   

            templateZIGridView.DataSource = templZiDT;
            templateZIGridView.DataBind();            
        }
    }

    protected void templateZIUseWeekFilterCheckBox_CheckedChanged(object sender, EventArgs e)
    {
        templateZiZiSaptaminaDropDownList.Enabled = templateZIUseWeekFilterCheckBox.Checked;
        FillTemplateZiGridView(templateZiConsulatSelector.SelectedValue, templateZiServiceDDL.SelectedValue, templateZiZiSaptaminaDropDownList.SelectedValue);
    }

    protected void templateZiZiSaptaminaDropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        FillTemplateZiGridView(templateZiConsulatSelector.SelectedValue, templateZiServiceDDL.SelectedValue, templateZiZiSaptaminaDropDownList.SelectedValue);
    }

    protected void templateZiConsulatSelector_SelectedIndexChanged(object sender, EventArgs e)
    {
        FillTemplateZiGridView(templateZiConsulatSelector.SelectedValue, templateZiServiceDDL.SelectedValue, templateZiZiSaptaminaDropDownList.SelectedValue);
    }

    protected void addNewTemplateZiButton_Click(object sender, EventArgs e)
    {
        ShowPanel(addNewTemplateZiPanel.ID);
    }
    
    protected void addNewTemplateZiSaveButton_Click(object sender, EventArgs e)
    {
        if (CheckServerObject())
        {
            int idConsulat = 0;
            int.TryParse(addNewTemplateZiConsulatDDL.SelectedValue, out idConsulat);

            int ziSaptamina = 0;
            int.TryParse(addNewTemplateZiZiSaptaminaDropDownList.SelectedValue, out ziSaptamina);

            int idService = 0;
            int.TryParse(addNewTemplateZiServiceCategoryDDL.SelectedValue, out idService);

            string startHour = addNewTemplateZiStartHourTextBox.Text;
            string endHour = addNewTemplateZiEndHourTextBox.Text;

            int maxPerson = -1;
            int.TryParse(addNewTemplateZiMaxPersonTextBox.Text, out maxPerson);
            
            if (mServerObject.AddNewTemplateZi(idConsulat, ziSaptamina, idService, startHour, endHour, maxPerson))
            {
                mServerObject.InfoText(this, "Data was Saved!");
                ShowPanel(templateZIPanel.ID);
                FillTemplateZiGridView(templateZiConsulatSelector.SelectedValue, templateZiServiceDDL.SelectedValue, templateZiZiSaptaminaDropDownList.SelectedValue);
            }
        }
    }

    protected void addNewTemplateZiCancelButton_Click(object sender, EventArgs e)
    {
        ShowPanel(templateZIPanel.ID);
    }

    protected void editTemplateZiCancelButton_Click(object sender, EventArgs e)
    {
        ShowPanel(templateZIPanel.ID);
    }

    protected void editTemplateZiSaveButton_Click(object sender, EventArgs e)
    {
        if (CheckServerObject())
        {
            int idTemplate = 0;
            int.TryParse(editTemplateZiIDLabel.Text, out idTemplate);

            int idConsulat = 0;
            int.TryParse(editTemplateZiConsulatDDL.SelectedValue, out idConsulat);

            int ziSaptamina = 0;
            int.TryParse(editTemplateZiZiSaptaminaDDL.SelectedValue, out ziSaptamina);

            int idService = 0;
            int.TryParse(editTemplateZiServiceCategoryDDL.SelectedValue, out idService);

            string startHour = editTemplateZiStartHourTextBox.Text;
            string endHour = editTemplateZiEndHourTextBox.Text;

            int maxPerson = -1;
            int.TryParse(editTemplateZiMaxPersonTextBox.Text, out maxPerson);

            if (mServerObject.UpdateTemplateZi(idTemplate, idConsulat, ziSaptamina, idService, startHour, endHour, maxPerson))
            {
                mServerObject.InfoText(this, "Data was Saved!");
                ShowPanel(templateZIPanel.ID);
                FillTemplateZiGridView(templateZiConsulatSelector.SelectedValue, templateZiServiceDDL.SelectedValue, templateZiZiSaptaminaDropDownList.SelectedValue);
            }
        }
    }

    protected void templateZIGridView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        ShowPanel(editTemplateZiPanel.ID);

        int rowIndex = e.NewEditIndex;

        if (CheckServerObject())
        {
            try
            {
                editTemplateZiIDLabel.Text = templateZIGridView.Rows[rowIndex].Cells[0].Text;
                editTemplateZiConsulatDDL.SelectedValue = templateZIGridView.Rows[rowIndex].Cells[1].Text;
                editTemplateZiZiSaptaminaDDL.SelectedValue = templateZIGridView.Rows[rowIndex].Cells[2].Text;

                int idService = 0;
                int.TryParse(templateZIGridView.Rows[rowIndex].Cells[3].Text, out idService);
                
                DataTable categoryList = mServerObject.GetServicesCategoryList();
                mServerObject.fillSelector(editTemplateZiServiceCategoryDDL, categoryList, "Denumirea", "ID Service");

                if (idService != 0)
                {
                    editTemplateZiServiceCategoryDDL.SelectedValue = idService.ToString();
                }

                DateTime startHour = new DateTime(1900, 1, 1);
                DateTime endHour = new DateTime(1900, 1, 1);

                DateTime.TryParse(templateZIGridView.Rows[rowIndex].Cells[5].Text, out startHour);
                DateTime.TryParse(templateZIGridView.Rows[rowIndex].Cells[6].Text, out endHour);

                editTemplateZiStartHourTextBox.Text = startHour.ToString("HH:mm");
                editTemplateZiEndHourTextBox.Text = endHour.ToString("HH:mm");

                editTemplateZiMaxPersonTextBox.Text = templateZIGridView.Rows[rowIndex].Cells[7].Text;
            }
            catch { }
        }
    }

    protected void templateZIGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int rowIndex = e.RowIndex;
        if (CheckServerObject())
        {
            int idTemplate = 0;
            int.TryParse(templateZIGridView.Rows[rowIndex].Cells[0].Text, out idTemplate);

            if (mServerObject.DeleteTemplateZi(idTemplate))
            {
                mServerObject.InfoText(this, "Data was Deleted!");
                ShowPanel(templateZIPanel.ID);
                FillTemplateZiGridView(templateZiConsulatSelector.SelectedValue, templateZiServiceDDL.SelectedValue, templateZiZiSaptaminaDropDownList.SelectedValue);
            }
        }
    }

    protected void templateZiUseServiceFilterCheckBox_CheckedChanged(object sender, EventArgs e)
    {
        templateZiServiceDDL.Enabled = templateZiUseServiceFilterCheckBox.Checked;
        FillTemplateZiGridView(templateZiConsulatSelector.SelectedValue, templateZiServiceDDL.SelectedValue, templateZiZiSaptaminaDropDownList.SelectedValue);
    }

    protected void templateZiServiceDDL_SelectedIndexChanged(object sender, EventArgs e)
    {
        FillTemplateZiGridView(templateZiConsulatSelector.SelectedValue, templateZiServiceDDL.SelectedValue, templateZiZiSaptaminaDropDownList.SelectedValue);
    }

    protected void templateZIGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        templateZIGridView.PageIndex = e.NewPageIndex;
        FillTemplateZiGridView(templateZiConsulatSelector.SelectedValue, templateZiServiceDDL.SelectedValue, templateZiZiSaptaminaDropDownList.SelectedValue);
    }

    protected void addNewTemplateFromDefaultTemplateSaveButton_Click(object sender, EventArgs e)
    {
        int idConsulat = 0;
        int.TryParse(addNewTemplateZiConsulatDDL.SelectedValue, out idConsulat);

        if (idConsulat != 0 )
        {
            if (mServerObject.CheckIfConsulatNotHaveTemplateZi(idConsulat))
            {
                if (mServerObject.InsertDefaultTemplates(addNewTemplateFromDefaultTemplateDDL.SelectedValue, idConsulat))
                {
                    FillTemplateZiGridView(templateZiConsulatSelector.SelectedValue, templateZiServiceDDL.SelectedValue, templateZiZiSaptaminaDropDownList.SelectedValue);
                    ShowPanel(templateZIPanel.ID);
                }
                else
                { 
                    mServerObject.InfoText(this, "In Procesul Aplicarii Templat-ului au fost comise erori! Incercati mai torziu!");
                }
            }
            else
            {
                mServerObject.InfoText(this, "Acest Consulat are deja TEMPLAIT-uri prestabilite!");
            }
        }
    }

    protected void addNewTemplateFromDefaultTemplateCancelButton_Click(object sender, EventArgs e)
    {
        ShowPanel(templateZIPanel.ID);
    }

    protected void SelectOneMode()
    {
        Label33.Enabled = addNewTemplateZiOneBYOneRadioButton.Checked;
        addNewTemplateZiZiSaptaminaDropDownList.Enabled = addNewTemplateZiOneBYOneRadioButton.Checked;
        Label34.Enabled = addNewTemplateZiOneBYOneRadioButton.Checked;
        addNewTemplateZiServiceCategoryDDL.Enabled = addNewTemplateZiOneBYOneRadioButton.Checked;
        Label39.Enabled = addNewTemplateZiOneBYOneRadioButton.Checked;
        addNewTemplateZiStartHourTextBox.Enabled = addNewTemplateZiOneBYOneRadioButton.Checked;
        Label46.Enabled = addNewTemplateZiOneBYOneRadioButton.Checked;
        addNewTemplateZiEndHourTextBox.Enabled = addNewTemplateZiOneBYOneRadioButton.Checked;
        Label47.Enabled = addNewTemplateZiOneBYOneRadioButton.Checked;
        addNewTemplateZiMaxPersonTextBox.Enabled = addNewTemplateZiOneBYOneRadioButton.Checked;
        addNewTemplateZiSaveButton.Enabled = addNewTemplateZiOneBYOneRadioButton.Checked;
        addNewTemplateZiCancelButton.Enabled = addNewTemplateZiOneBYOneRadioButton.Checked;


        Label7.Enabled = addNewTemplateFromDefaultTemplateRadioButton.Checked;
        Label6.Enabled = addNewTemplateFromDefaultTemplateRadioButton.Checked;
        addNewTemplateFromDefaultTemplateDDL.Enabled = addNewTemplateFromDefaultTemplateRadioButton.Checked;
        addNewTemplateFromDefaultTemplateSaveButton.Enabled = addNewTemplateFromDefaultTemplateRadioButton.Checked;
        addNewTemplateFromDefaultTemplateCancelButton.Enabled = addNewTemplateFromDefaultTemplateRadioButton.Checked;
    }

    protected void addNewTemplateZiOneBYOneRadioButton_CheckedChanged(object sender, EventArgs e)
    {
        addNewTemplateFromDefaultTemplateRadioButton.Checked = !addNewTemplateZiOneBYOneRadioButton.Checked;
        SelectOneMode();
    }

    protected void addNewTemplateFromDefaultTemplateRadioButton_CheckedChanged(object sender, EventArgs e)
    {
        addNewTemplateZiOneBYOneRadioButton.Checked = !addNewTemplateFromDefaultTemplateRadioButton.Checked;
        SelectOneMode();
    }
    
    #endregion Template ZI
    
    #region  CALENDAR

    protected void FillCalendarGridView(string idConsulat, string dateStringFrom, string dateStringTo)
    {
        int intIDConsulat = 0;
        int.TryParse(idConsulat, out intIDConsulat);

        if (CheckServerObject())
        {
            DateTime dateFrom = new DateTime(1900, 1, 1);
            if (!dateStringFrom.Equals(string.Empty)) DateTime.TryParse(dateStringFrom, out dateFrom);

            DateTime dateTo = new DateTime(1900, 1, 1);
            if (!dateStringTo.Equals(string.Empty)) DateTime.TryParse(dateStringTo, out dateTo);

            DataTable Calendar = mServerObject.GetCalendarList(intIDConsulat, dateFrom, dateTo);
            
            calendarGridView.DataSource = Calendar;
            calendarGridView.DataBind();    
        }
    }

    protected void calendarShowButton_Click(object sender, EventArgs e)
    {
        FillCalendarGridView(calendarConsulatDDL.SelectedValue, calendarDateFromTextBox.Text, calendarDateToTextBox.Text);       
    }

    protected void calendarGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int rowIndex = e.RowIndex;

        if (CheckServerObject())
        {
            string stringIdCalendar = calendarGridView.Rows[rowIndex].Cells[1].Text;
            int idCalendar = 0;
            int.TryParse(stringIdCalendar, out idCalendar);

            if (idCalendar != 0 && mServerObject.DeleteCalendarByID(idCalendar))
            {
                ShowPanel(calendarPanel.ID);
                FillCalendarGridView(calendarConsulatDDL.SelectedValue, calendarDateFromTextBox.Text, calendarDateToTextBox.Text);  
            }
            else
                mServerObject.InfoText(this, "Error Deleting record!");
        }
    }

    protected void calendarGridView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int rowIndex = e.NewEditIndex;

        if (CheckServerObject())
        {
            editCalendarIDLabel.Text = calendarGridView.Rows[rowIndex].Cells[1].Text;
            editCalendarConsulatLabel.Text = calendarGridView.Rows[rowIndex].Cells[3].Text;

            Label label = (Label)calendarGridView.Rows[rowIndex].FindControl("Datalabel");   
            if(label != null)
                editCalendarDataTextBox.Text = label.Text;

            editCalendarZiSaptaminaLabel.Text = calendarGridView.Rows[rowIndex].Cells[5].Text;
            editCalendarServiceLabel.Text = calendarGridView.Rows[rowIndex].Cells[6].Text;
            editCalendarStartEndHoursLabel.Text = calendarGridView.Rows[rowIndex].Cells[7].Text + " - " + calendarGridView.Rows[rowIndex].Cells[8].Text;
            editCalendarLocuriLibereTextBox.Text = calendarGridView.Rows[rowIndex].Cells[9].Text;

            CheckBox cb = (CheckBox)calendarGridView.Rows[rowIndex].FindControl("StatusAprobare");
            if (cb != null)
            {
                editCalendarStatusAprobareCheckBox.Checked = cb.Checked;
            }
           
            ShowPanel(editCalendarPanel.ID);
        }
    }

    protected void editCalendarSaveButton_Click(object sender, EventArgs e)
    {
        if (CheckServerObject())
        {
            int idCalendar = 0;
            int.TryParse(editCalendarIDLabel.Text, out idCalendar);

            DateTime dataIdCalendar = new DateTime(1900,1,1);
            DateTime.TryParse(editCalendarDataTextBox.Text, out dataIdCalendar);

            int locuriLibere = 0;
            int.TryParse(editCalendarLocuriLibereTextBox.Text, out locuriLibere);

            if (mServerObject.UpdateCalendarByID(idCalendar, dataIdCalendar, locuriLibere, editCalendarStatusAprobareCheckBox.Checked))
            {
                mServerObject.InfoText(this, "Data was Saved!");
                FillCalendarGridView(calendarConsulatDDL.SelectedValue, calendarDateFromTextBox.Text, calendarDateToTextBox.Text);     
            }
        }

        ShowPanel(calendarPanel.ID);
    }

    protected void editCalendarCancelButton_Click(object sender, EventArgs e)
    {
        ShowPanel(calendarPanel.ID);
    }

    protected void fillFromTemplateSaveButton_Click(object sender, EventArgs e)
    {
        if (CheckServerObject())
        {
            DateTime dateFrom = new DateTime();
            DateTime.TryParse(fillCalendarFromTemplateBeginFromTextBox.Text, out dateFrom);

            DateTime dateTo = new DateTime();
            DateTime.TryParse(fillCalendarFromTemplateEndTextBox.Text, out dateTo);

            int dayCounter = (dateTo - dateFrom).Days;

            int consulatID = 0;
            int.TryParse(fillFromTemplateConsulatDDL.SelectedValue, out consulatID);

            int serviceID = 0;
            int.TryParse(fillFromTemplateServiceDDL.SelectedValue, out serviceID);

            if (mServerObject.FillCalendarFromTemplate(consulatID, dayCounter, serviceID))
            {
                mServerObject.InfoText(this, "Data was filled succesiful!");
            }

            calendarConsulatDDL.SelectedValue = fillFromTemplateConsulatDDL.SelectedValue;
            FillCalendarGridView(calendarConsulatDDL.SelectedValue, calendarDateFromTextBox.Text, calendarDateToTextBox.Text);     
            ShowPanel(calendarPanel.ID);
        }
    }

    protected void fillFromTemplateCancelButton_Click(object sender, EventArgs e)
    {
        ShowPanel(calendarPanel.ID);
    }

    protected void fillFromTemplateButton_Click(object sender, EventArgs e)
    {
        ShowPanel(fillFromTemplatePanel.ID);
    }

    protected void calendarConsulatDDL_TextChanged(object sender, EventArgs e)
    {
        FillCalendarGridView(calendarConsulatDDL.SelectedValue, calendarDateFromTextBox.Text, calendarDateToTextBox.Text);     
    }

    protected void calendarSelectAllRecordsCheckBox_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < calendarGridView.Rows.Count; i++)
        {
            CheckBox cb = (CheckBox)calendarGridView.Rows[i].FindControl("StatusAprobare");
            cb.Checked = calendarSelectAllRecordsCheckBox.Checked;           
        }
    }

    protected void calendarApproveButton_Click(object sender, EventArgs e)
    {
        if (CheckServerObject())
        {
            for (int i = 0; i < calendarGridView.Rows.Count; i++)
            {
                CheckBox cb = (CheckBox)calendarGridView.Rows[i].FindControl("StatusAprobare");

                if (cb != null)
                {                    
                    int idCalendar = 0;
                    int.TryParse(calendarGridView.Rows[i].Cells[1].Text, out idCalendar);

                    DateTime dataIdCalendar = new DateTime(1900, 1, 1);

                    Label dataLabel = (Label)calendarGridView.Rows[i].FindControl("DataLabel");
                    if (dataLabel != null)
                        DateTime.TryParse(dataLabel.Text, out dataIdCalendar);

                    int locuriLibere = 0;
                    int.TryParse(calendarGridView.Rows[i].Cells[9].Text, out locuriLibere);

                    bool statusAprobare = cb.Checked;

                    mServerObject.UpdateCalendarByID(idCalendar, dataIdCalendar, locuriLibere, statusAprobare);
                }
            }

            FillCalendarGridView(calendarConsulatDDL.SelectedValue, calendarDateFromTextBox.Text, calendarDateToTextBox.Text);
        }
    }

    protected void saveStatutAprobareFromPeriodButton_Click(object sender, EventArgs e)
    {
        if (CheckServerObject())
        {
            int idConsulat = 0;
            int.TryParse(calendarConsulatDDL.SelectedValue, out idConsulat);

            DateTime dateFrom = new DateTime(1900,1,1);
            DateTime dateTo = new DateTime(1900,1,1);

            DateTime.TryParse(calendarDateFromTextBox.Text.Trim(), out dateFrom);
            DateTime.TryParse(calendarDateToTextBox.Text.Trim(), out dateTo);

            DataTable calendarList = mServerObject.GetCalendarList(idConsulat, dateFrom, dateTo);

            bool statusAprobare = calendarSelectAllRecordsFromPeriodCheckBox.Checked;

            if (!calendarDateFromTextBox.Text.Equals(string.Empty) && !calendarDateToTextBox.Text.Equals(string.Empty) && calendarList != null)
                for (int i = 0; i < calendarList.Rows.Count; i++)
                {
                    int idCalendar = (int)calendarList.Rows[i]["ID Calendar"];

                    DateTime dataIdCalendar = new DateTime(1900, 1, 1);

                    if (!calendarList.Rows[i]["Data"].ToString().Equals(string.Empty))
                        dataIdCalendar = (DateTime)calendarList.Rows[i]["Data"];

                    int locuriLibere = (int)calendarList.Rows[i]["Locuri Libere"];                    

                    mServerObject.UpdateCalendarByID(idCalendar, dataIdCalendar, locuriLibere, statusAprobare);                   
                }

            FillCalendarGridView(calendarConsulatDDL.SelectedValue, calendarDateFromTextBox.Text, calendarDateToTextBox.Text);
        }
    }

    protected void calendarGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        calendarGridView.PageIndex = e.NewPageIndex;
        FillCalendarGridView(calendarConsulatDDL.SelectedValue, calendarDateFromTextBox.Text, calendarDateToTextBox.Text);
    }

    #endregion  CALENDAR

    #region Sarbatori

    protected void FillHolydaysGridView(string idConsulat)
    {
        int intIDConsulat = 0;
        int.TryParse(idConsulat, out intIDConsulat);

        if (CheckServerObject())
        {            
            DataTable holy = mServerObject.GetSarbatoriList(intIDConsulat);
            holidaysGridView.DataSource = holy;
            holidaysGridView.DataBind();
        }
    }

    protected void addNewHoliday_Click(object sender, EventArgs e)
    {
        ShowPanel(addNewHolidayPanel.ID);
    }

    protected void holidaysGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int rowIndex = e.RowIndex;

        if (CheckServerObject())
        {
            int idConsulat = 0;
            int.TryParse(holidaysGridView.Rows[rowIndex].Cells[0].Text, out idConsulat);

            Label dataLabel = (Label)holidaysGridView.Rows[rowIndex].Cells[2].FindControl("Label1");

            if (dataLabel != null)
            {
                DateTime inputData = new DateTime(1900, 1, 1);
                DateTime.TryParse(dataLabel.Text, out inputData);

                if (mServerObject.DeleteHolyDay(idConsulat, inputData))
                {
                    mServerObject.InfoText(this, "Data was Deleted!");
                    ShowPanel(HolyDaysPanel.ID);
                    FillHolydaysGridView(holidayConsulatDDL.SelectedValue);
                }
            }
        }
    }

    protected void holidaysGridView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int rowIndex = e.NewEditIndex;
        ShowPanel(editHolidaysPanel.ID);

        if (CheckServerObject())
        {
            editHolidaysConsulatDDL.SelectedValue = holidaysGridView.Rows[rowIndex].Cells[0].Text;

            Label label = (Label)holidaysGridView.Rows[rowIndex].FindControl("Label1");
            editHolidaysDataTextBox.Text = label.Text;

            DateTime startHour = new DateTime(1900, 1, 1);
            DateTime endHour = new DateTime(1900, 1, 1);

            DateTime.TryParse(holidaysGridView.Rows[rowIndex].Cells[3].Text, out startHour);
            DateTime.TryParse(holidaysGridView.Rows[rowIndex].Cells[4].Text, out endHour);

            editHolyDaysWorkTimeFromTextBox.Text = startHour.ToString("HH:mm");
            editHolyDaysWorkTimeToTextBox.Text = endHour.ToString("HH:mm");

            editHolyDaysWorkHoursTextBox.Text = holidaysGridView.Rows[rowIndex].Cells[5].Text;
        }
    }

    protected void holidayConsulatDDL_SelectedIndexChanged(object sender, EventArgs e)
    {
        FillHolydaysGridView(holidayConsulatDDL.SelectedValue);
    }

    protected void editHolyDaysSaveButton_Click(object sender, EventArgs e)
    {
        if (CheckServerObject())
        {
            int idConsulat = 0;
            int.TryParse(editHolidaysConsulatDDL.SelectedValue, out idConsulat);

            DateTime inputData = new DateTime(1900,1,1);
            DateTime.TryParse(editHolidaysDataTextBox.Text, out inputData);

            int workHours = 0;
            int.TryParse(editHolyDaysWorkHoursTextBox.Text, out workHours);

            if (mServerObject.UpdateHolyDay(idConsulat, inputData, editHolyDaysWorkTimeFromTextBox.Text, editHolyDaysWorkTimeToTextBox.Text, workHours))
            {
                mServerObject.InfoText(this, "Data was Saved!");
                ShowPanel(HolyDaysPanel.ID);
                FillHolydaysGridView(holidayConsulatDDL.SelectedValue);
            }
        }
    }

    protected void editHolyDaysCancelButton_Click(object sender, EventArgs e)
    {
        ShowPanel(HolyDaysPanel.ID);
    }

    protected void addNewHolidaySaveButton_Click(object sender, EventArgs e)
    {
        if (CheckServerObject())
        {
            int idConsulat = 0;
            int.TryParse(addNewHolidayConsulatDDL.SelectedValue, out idConsulat);

            DateTime inputData = new DateTime(1900, 1, 1);
            DateTime.TryParse(addNewHolidayDataTextBox.Text, out inputData);

            int workHours = 0;
            int.TryParse(addNewHolidayWorkHoursTextBox.Text, out workHours);

            if (mServerObject.AddNewHolyDay(idConsulat, inputData, addNewHolidayWorkTimeFromTextBox.Text, addNewHolidayWorkTimeToTextBox.Text, workHours))
            {
                mServerObject.InfoText(this, "Data was Saved!");
                ShowPanel(HolyDaysPanel.ID);
                FillHolydaysGridView(holidayConsulatDDL.SelectedValue);
            }
        }
    }

    protected void addNewHolidayCancelButton_Click(object sender, EventArgs e)
    {
        ShowPanel(HolyDaysPanel.ID);
    }

    protected void holidaysGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        holidaysGridView.PageIndex = e.NewPageIndex;
        FillHolydaysGridView(holidayConsulatDDL.SelectedValue);
    }

    #endregion Sarbatori

    protected void FillVisualizareProgramarilorGridView()
    {        
        int idConsulat = 0;
        int.TryParse(VisualizareProgramariConsulatDDL.SelectedValue, out idConsulat);

        DateTime selectedDate = VisualizareProgramariCalendar.SelectedDate;

        TimeSpan time = new TimeSpan();
        TimeSpan.TryParse(VisualizareProgramariOraTextBox.Text.Split(' ')[0], out time);

        int idService = 0;
        int.TryParse(VisualizareProgramariServiceDDL.SelectedValue, out idService);

        bool useTimeFilter = VisualizareProgramariCheckBox.Checked;
        bool useServiceFilter = VisualizareProgramariServisCheckBox.Checked;

        DataTable sourceTable = mServerObject.GetRegistredPersonByDay(idConsulat, selectedDate, time, useTimeFilter, idService, useServiceFilter);

        if (sourceTable != null && sourceTable.Rows.Count > 0)
        {
            VisualizareProgramariGridView.DataSource = sourceTable;
            VisualizareProgramariGridView.DataBind();
        }
        else
        {
            VisualizareProgramariGridView.DataSource = null;
            VisualizareProgramariGridView.DataBind();
        }
    }

    protected void VisualizareProgramariCalendar_SelectionChanged(object sender, EventArgs e)
    {
        FillVisualizareProgramarilorGridView();
    }
    
    protected void VisualizareProgramariCheckBox_CheckedChanged(object sender, EventArgs e)
    {
        VisualizareProgramariOraTextBox.Enabled = VisualizareProgramariCheckBox.Checked;
        okFilterTimeButton.Enabled = VisualizareProgramariCheckBox.Checked;
        FillVisualizareProgramarilorGridView();
    }

    protected void VisualizareProgramariServisCheckBox_CheckedChanged(object sender, EventArgs e)
    {
        VisualizareProgramariServiceDDL.Enabled = VisualizareProgramariServisCheckBox.Checked;
        FillVisualizareProgramarilorGridView();
    }

    protected void VisualizareProgramariServiceDDL_SelectedIndexChanged(object sender, EventArgs e)
    {
        FillVisualizareProgramarilorGridView();
    }

    protected void calendarSelectAllRecordsFromPeriodCheckBox_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < calendarGridView.Rows.Count; i++)
        {
            CheckBox cb = (CheckBox)calendarGridView.Rows[i].FindControl("StatusAprobare");
            cb.Checked = calendarSelectAllRecordsFromPeriodCheckBox.Checked;
        }
    }
    
    protected void fillFromTemplateConsulatDDL_SelectedIndexChanged(object sender, EventArgs e)
    {
        FillDateFromTo();
    }

    protected void fillFromTemplateServiceDDL_SelectedIndexChanged(object sender, EventArgs e)
    {
        FillDateFromTo();
    }        
}


    
